<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${basePackage}.adapter.driving.persistence.${module}.mapper.${className}Mapper">

  <insert id="insert" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO ${tableName}
    (
    <#if fields??>
      <#assign insertFields = fields?filter(field -> !field.ignoreInsert)>
      <#list insertFields as field>
      ${field.columnName}<#if field_has_next>,</#if>
      </#list>
    </#if>
    )
    VALUES
    (
    <#if fields??>
      <#list insertFields as field>
        <#if field.dateField>
      NOW()<#if field_has_next>,</#if>
          <#else>
      ${r"#{"}${field.fieldName}${r"}"}<#if field_has_next>,</#if>
        </#if>
      </#list>
    </#if>
    )
  </insert>

  <delete id="deleteById">
    DELETE
    FROM ${tableName}
    WHERE id = ${r"#{id}"}
  </delete>

  <select id="selectById" resultType="${basePackage}.adapter.driving.persistence.${module}.po.${className}Po">
    SELECT
    <#if fields??>
      <#list fields as field>
      ${field.columnName}<#if field_has_next>,</#if>
      </#list>
    </#if>
    FROM ${tableName}
    WHERE id = ${r"#{id}"}
  </select>

  <update id="updateById">
    UPDATE ${tableName}
    SET
    <#if fields??>
    <#assign updateFields = fields?filter(field -> !field.ignoreUpdate)>
      <#list updateFields as field>
        <#if field.dateField>
      ${field.columnName} = NOW()<#if field_has_next>,</#if>
        <#else>
      ${field.columnName} = ${r"#{"}${field.fieldName}${r"}"}<#if field_has_next>,</#if>
       </#if>
      </#list>
    </#if>
    WHERE id = ${r"#{id}"}
  </update>

</mapper>
